package com.zhuifeng.smart.ajax;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.zhuifeng.smart.model.User;
import com.zhuifeng.smart.services.UserServices;
import com.zhuifeng.smart.services.impl.UserServicesImpl;

/**
 * Servlet implementation class UserAjax
 */
@WebServlet("/user_ajax.do")
public class UserAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserAjax() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		String type = request.getParameter("type");
		if ("randImage".equals(type)) {
			this.checkRandImage(request, response);
			return;
		} else if ("isHaveUser".equals(type)) {
			this.isHaveUser(request, response);
			return;
		} else if ("register".equals(type)) {
			this.register(request, response);
			return;
		} else if ("login".equals(type)) {
			this.login(request, response);
			return;
		} else if ("logout".equals(type)) {
			this.logout(request, response);
			return;
		}
	}

	// 检查验证码
	private void checkRandImage(HttpServletRequest request,
			HttpServletResponse response) {
		String randNum = request.getParameter("randNum");
		HttpSession session = request.getSession(true);
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String s = (String) session.getAttribute("rand");
		out.print(randNum.equals(s));
		out.close();
	}

	// 检查用户是否存在
	private void isHaveUser(HttpServletRequest request,
			HttpServletResponse response) {
		String userid = request.getParameter("userid");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		UserServices userDao = new UserServicesImpl();
		boolean isHaveUser = userDao.isHaveUser(userid);
		out.print(isHaveUser);
		out.close();
	}

	// 登录
	private void login(HttpServletRequest request, HttpServletResponse response) {
		String userAccount = request.getParameter("userid");
		String userPassword = request.getParameter("userPassword");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		UserServices userDao = new UserServicesImpl();
		User isLogin = userDao.login(userAccount, userPassword);
		if (isLogin != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("userInfo", isLogin);
			System.out.println(isLogin.getUsername());
			String script = "{msg:1,username:'" + isLogin.getUsername()
					+ "',userlevel:'" + isLogin.getUserlevel() + "'}";
			out.print(script);
		} else {
			String script = "{msg:2}";
			out.print(script);
		}
		out.close();
	}

	// 注册
	private void register(HttpServletRequest request,
			HttpServletResponse response) {
		String userAccount = request.getParameter("userid");
		String userPassword = request.getParameter("userPassword");
		String userName = request.getParameter("userName");

		UserServices userDao = new UserServicesImpl();
		User isInsert = userDao.register(new User(userAccount, userPassword,
				userName));
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (isInsert != null) {
			HttpSession session = request.getSession(true);
			session.setAttribute("userInfo", isInsert);
			String script = "{msg:1,username:'" + userName + "'}";
			out.print(script);
		} else {
			String script = "{msg:2}";
			out.print(script);
		}
		out.close();
	}

	// 注销
	private void logout(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		session.removeAttribute("userInfo");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(true);
		out.close();
	}
}
